package com.feitubc.tools.fileimport.demo.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.feitubc.tools.fileimport.IDealSingleService;
import com.feitubc.tools.fileimport.domain.ImportFileResultEnum;
import com.feitubc.tools.fileimport.domain.ImportLogObject;
import com.feitubc.tools.fileimport.util.DateHelper;
import com.google.common.collect.Maps;

/**
 * 说明：处理单条请求接口实现
 * 配置示例1-1, 库存调整多线程配置，检验登录人信息，校验文件名称，记录日志，使用多线程
 * @author zhc
 */
@Service
public class ImportStockStrategyMultiThreadService implements IDealSingleService {
	
	protected final Logger logger = Logger.getLogger(this.getClass());

	@Override
	public List<ImportLogObject> preExecuteData(List<ImportLogObject> importLogObjects) {
		// TODO Auto-generated method stub
		return IDealSingleService.super.preExecuteData(importLogObjects);
	}

	@Override
	public boolean preData(ImportLogObject importLogObject) {
		boolean flag = false;
		try {
			if (importLogObject != null) {
				Map<String, Object> objData = Maps.newHashMap();
				
				Map<Integer, Object> dataMap = importLogObject.getDataMap();
				Object codeObj = dataMap.get(0);
				String warehouseCode = "";
				if (codeObj == null) {
					importLogObject.setStatusByResultEnum(ImportFileResultEnum.FILE_PARAM_ERROR, "仓库编号为空");
					return flag;
				} else {
					warehouseCode = codeObj.toString();
					objData.put("warehouseCode", warehouseCode);
				}
				
				// 设置业务序号
				importLogObject.setBussinessNo(warehouseCode);
				// 设置业务编码
				importLogObject.setCode(warehouseCode);
				
				/**
				 * 
				 * 解析数据
				 * 
				 */
				
				Object productDateObj = dataMap.get(14);
				Date productDate = null;
				if (productDateObj != null) {
					productDate = DateHelper.parseDate(productDateObj.toString(), DateHelper.DATE_FORMAT);
				}
				if (productDate == null) {
					importLogObject.setStatusByResultEnum(ImportFileResultEnum.FILE_PARAM_ERROR, "生产日期为空");
					return flag;
				}
				
				
				// ！！！重要！！！  需要将解析后的数据放入到 data 里面，后续作数据有效校验会使用
				importLogObject.setData(objData);
				flag = true;
			}
		} catch (Exception e) {
			importLogObject.setStatusByResultEnum(ImportFileResultEnum.CONV_PARAM_ERROR, e.getMessage());
			this.logger.error("ImportStockStrategyService#preData Exception :", e);
		}
		return flag;
	}

	@Override
	public boolean execute(ImportLogObject importLogObject) {
		// TODO 拿到数据后，可以做一些业务操作
		Object objData = importLogObject.getData();
		System.out.println(objData);
		return true;
	}
	
}
